package org.hagen.mmia.yars.engine.corpora;

import java.util.StringTokenizer;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class ClefIPHandler extends DefaultHandler {

	StringBuffer accumulator = new StringBuffer(); // Accumulate parsed text

    public String languageCode = null;
    public String patentNumber = null;	
    public String title = null;
    public String content = null;	
    public String description = null;	
    
    private boolean accumuleTitle = false;
    private boolean accumuleContent = false;
    private boolean accumuleDescription = false;
	private boolean accumule = true;

	private boolean normalized = false;
	
	private int N = 500;  // window of text to be output around the reference strings
	public StringBuffer accumulatedText = null;
	private StringBuffer allContent = null;	
	private static String fullPunctuations = new String("([ ,:;?.!/)-\u2013\"\u201c\u201d\u2018\u2019\'`$]*\u2666\u2665\u2663\u2660");
	private static String delimiters = " \n\t" + fullPunctuations;
	
	public ClefIPHandler() {
	}

	public void characters(char[] buffer, int start, int length) {
		if (accumule) {
			accumulator.append(buffer, start, length);
		}
	}

	public String getText() {
		String text = accumulator.toString().trim();
		text = text.replace("\n", " ");
		text = text.replace("\t", " ");
		text = text.replaceAll("\\p{Space}+", " ");
		return text;
	}

	public void endElement(String uri, String localName, String qName)
			throws SAXException {

		if (qName.equals("abstract")) {
			
			if( accumuleContent && content == null ) {
				content = getText();
			}
    		accumuleContent = false;
    		accumule	    = false;
			
		} else if (qName.equals("invention-title")) {

			if( accumuleTitle && title == null) {
				title = getText();
			}
    		accumuleTitle = false;
    		accumule	  = false;
		} else if (qName.equals("description")) {

			if( accumuleDescription && description == null ) {

	            String content = getText();
                // we tokenize the text
                StringTokenizer st = new StringTokenizer(content, delimiters, true);

                int i = 0;
                while (st.hasMoreTokens()) {

                	String token = st.nextToken().trim();
                    if (token == null || token.length() == 0) {
                        continue;
                    }
                    
                    // we print only a window of N words
                    if (i < N) {
                        if (token.length() > 0) {
                            accumulatedText.append(token + "\t" + "<ignore>\n");
                            allContent.append(token + " ");
	                    }
	                }
	                    
	                i++;
	            }
                accumulator.setLength(0);
                
                description = allContent.toString();
			
			}
    		accumuleDescription = false;
    		accumule	        = false;
		}
	}

	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) throws SAXException {

		if (qName.equals("patent-document")) {
			int length = atts.getLength();

            accumulatedText = new StringBuffer();
            allContent = new StringBuffer();
			
            // Process each attribute
            for (int i = 0; i < length; i++) {
            	
                // Get names and values for each attribute
                String name = atts.getQName(i);
                String value = atts.getValue(i);

                if (name != null) {
                    if (name.equals("lang")) {
                        languageCode = value;
                    }
                    if (name.equals("doc-number")) {
                        patentNumber = "EP" + value;
                    }
        		}
            }

		} else if( qName.equals("invention-title")) {
        	
        	int length = atts.getLength();
        	
            // Process each attribute
            for (int i = 0; i < length; i++) {
            	
                // Get names and values for each attribute
                String name = atts.getQName(i);
                String value = atts.getValue(i);

                if (name != null) {
                    if (name.equals("lang")) {

                    	if( value.equals( "EN" ) ) {
                    		accumuleTitle = true;
                    		accumule	  = true;
                    	}
                    }
        		}
            }
        	
        	accumulator.setLength(0);

        } else if( qName.equals("abstract")) {

        	int length = atts.getLength();
        	
            // Process each attribute
            for (int i = 0; i < length; i++) {
            	
                // Get names and values for each attribute
                String name = atts.getQName(i);
                String value = atts.getValue(i);

                if (name != null) {
                    if (name.equals("lang")) {

                    	if( value.equals( "EN" ) ) {
	                		accumuleContent = true;
	                		accumule	    = true;
                    	}
                    }
        		}
            }
        	
        	accumulator.setLength(0);
        } else if( qName.equals("description")) {

        	int length = atts.getLength();
        	
            // Process each attribute
            for (int i = 0; i < length; i++) {
            	
                // Get names and values for each attribute
                String name = atts.getQName(i);
                String value = atts.getValue(i);

                if (name != null) {
                    if (name.equals("lang")) {

                    	if( value.equals( "EN" ) ) {
	                		accumuleDescription = true;
	                		accumule	        = true;
                    	}
                    }
        		}
            }
        	
        	accumulator.setLength(0);
        }
	}

	/*
	 * transformation from temporary into actual data
	 */
	public void normalize( boolean stop, boolean stem ) {

		// normalize title ///////////////
		String line = title;
		
		// replace special characters
		line = line.replaceAll("[\\,.\\s/\\-()\"='0-9%]+", " ");
	
		// stopping
		if( stop ) {
			line = Reader.doStopping( line );
		}
		// stemming
		if( stem ) {
			line = Reader.doStemming( line );
		}
		
		title = line.trim();

		// normalize content ///////////////
		line = content;
		if( line != null ) {
				
			// replace special characters
			line = line.replaceAll("[\\,.\\s/\\-()\"='0-9%]+", " ");
	
			// stopping
			if( stop ) {
				line = Reader.doStopping( line );
			}
			// stemming
			if( stem ) {
				line = Reader.doStemming( line );
			}
			
			content = line.trim();
		}
		
		// normalize description ///////////////
		line = description;
		if( line != null ) {
	
			// replace special characters
			line = line.replaceAll("[\\,.\\s/\\-()\"='0-9%]+", " ");
	
			// stopping
			if( stop ) {
				line = Reader.doStopping( line );
			}
			// stemming
			if( stem ) {
				line = Reader.doStemming( line );
			}
		
			description = line.trim();
		}
	}	
	
	public String getPatentNumber() {
		return patentNumber;
	}

	public String getTitle() {
		
		if( !normalized ) {
			this.normalize( Reader.STOPPING, Reader.STEMMING );
			normalized = true;
		}
		
		return title;
	}

	public String getContent() {
		if( !normalized ) {
			this.normalize( Reader.STOPPING, Reader.STEMMING );
			normalized = true;
		}

		return content;
	}

	public String getDescription() {
		if( !normalized ) {
			this.normalize( Reader.STOPPING, Reader.STEMMING );
			normalized = true;
		}

		return description;
	}
}
